package servlets;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import dao.BiedingDao;
import dao.GebruikerDao;
import dao.VeilingDao;
import db.BuildDB;
import db.DBManager;

public class ContextListener implements ServletContextListener
{

	@Override
	public void contextDestroyed(ServletContextEvent e)
	{

	}

	@Override
	public void contextInitialized(ServletContextEvent e)
	{
		System.out.println("Context initialized");
		// maak DBManager

		String databaseLocation = e.getServletContext().getInitParameter("database.location");
		databaseLocation = databaseLocation.replace("%home%", System.getProperty("user.home"));
		System.out.println("database location = " + databaseLocation);

		String databaseDrivers = e.getServletContext().getInitParameter("database.drivers");
		DBManager.setParameters(databaseDrivers, databaseLocation);
		DBManager manager = DBManager.getInstance();
		e.getServletContext().setAttribute("DBManager", manager);

		// maak Tables
		BuildDB.buildTables();

		// maak DAOs aan
		GebruikerDao gDao = new GebruikerDao(manager);
		VeilingDao vDao = new VeilingDao(manager);
		BiedingDao bDao = new BiedingDao(manager);
		e.getServletContext().setAttribute("gebruikersDao", gDao);
		e.getServletContext().setAttribute("veilingsDao", vDao);
		e.getServletContext().setAttribute("biedingsDao", bDao);

	}
}
